package org.umss.fixturebuilder.builder.data;

import java.util.ArrayList;
import java.util.List;
import org.umss.fixturebuilder.builder.model.Team;
import static org.umss.fixturebuilder.builder.data.OfyService.ofy;
import org.umss.fixturebuilder.model.appengine.ChampionshipModel;
import org.umss.fixturebuilder.model.appengine.TeamModel;
import com.googlecode.objectify.Key;

/**
 *
 * @author Daniela Meneses
 */
public class DBDataSource implements DataSource {

    private final String championshipName;

    DBDataSource(String championship) {
        this.championshipName = championship;
    }

    public List<Team> getTeams() throws DataSourceException {
        Key<ChampionshipModel> championshipKey = Key.create(ChampionshipModel.class, championshipName);
        List<TeamModel> teams = ofy().load().type(TeamModel.class).ancestor(championshipKey).list();
        List<Team> res = new ArrayList<Team>(teams.size());
        Team team;
        for (TeamModel teamModel : teams) {
            team = new Team(teamModel.getId(), teamModel.getName());
            res.add(team);
        }

        return res;
    }

}
